
import {
    baseUrlassets,
    baseUrlmember
} from './auth.js'

//console.log(baseUrlassets())

function getToken() {
    try {
        const token = uni.getStorageSync('userInfo').token;
        if (token) {
            return token
        } else {
            return ''
        }
    } catch (e) {
        console.log(e)
    }
}



function getHeaders() {
    let token = getToken()
    let header = {
        "Authorization": 'bearer ' + token, // 'X-Requested-With': 'XMLHttpRequest',
        // 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
    }
    return header
}

export default {

    postAssets(url, param) {
        return new Promise((resolve, reject) => {
            uni.request({
                method: 'POST',
                url: baseUrlassets() + url,
                data: param,
                header: getHeaders()
            }).then(res => {

                if (res[1] != undefined) {

                    if (res[1].data.status == '401') {
                        uni.navigateTo({
                            url: '/src/pages/login/login'
                        });
                    } else {
                        resolve(res[1].data)
                    }
                }
            }).catch(
                (response) => {
                    reject(response)
                }
            )
        })
    },
    postMember(url, param) {
        return new Promise((resolve, reject) => {
            uni.request({
                method: 'POST',
                url: baseUrlmember() + url,
                data: param,
                header: getHeaders()
            }).then(res => {

                if (res[1] != undefined) {

                    if (res[1].data.status == '401') {
                        uni.navigateTo({
                            url: '/src/pages/login/login'
                        });
                    } else {
                        resolve(res[1].data)
                    }
                }
            }).catch(
                (response) => {
                    reject(response)
                }
            )
        })
    },
    //登录，注册,忘记密码 不传token;
    postLoginris(url, param) {
        return new Promise((resolve, reject) => {
            uni.request({
                method: 'POST',
                url: baseUrlmember() + url,
                data: param,
            }).then(res => {

                if (res[1] != undefined) {
                    
                    if (res[1].data.status == '401') {
                        uni.navigateTo({
                            url: '/src/pages/login/login'
                        });
                    } else {
                        resolve(res[1].data)
                    }
                }

            }).catch(
                (response) => {
                    reject(response)
                }
            )
        })
    },
}
